package com.tedu.egmvc.controller;

import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDTO;
import com.tedu.egmvc.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@Controller
@RestController
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;
    @RequestMapping("/address/insert")
    public String insert(AddressDTO addressDTO){
        System.out.println("开始添加操作");
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        System.out.println(address);
        int num = addressMapper.insert(address);
        return num > 0 ? "添加成功":"添加失败";
    }
    @RequestMapping("/address/select")
    public List<Address> selectAll(){
        System.out.println("开始查询操作");
        return addressMapper.selectAll();
    }
    @RequestMapping("/address/delete")
    public String deleteAddress(Integer id){
        System.out.println("开始删除操作,被删除的地址id为:"+id);
        int num = addressMapper.deleteById(id);
        return num > 0 ? "删除成功":"删除失败";
    }
    @RequestMapping("/address/update")
    public String updateAddress(Address address){
        System.out.println("开始修改操作");
        int num = addressMapper.updateById(address);
        System.out.println(address);
        return num > 0 ? "修改成功":"修改失败";
    }
}
